package main

import (
	"log"

	"github.com/gin-gonic/gin"

	"recruit_student_backend/internal/handler"
	"recruit_student_backend/internal/middleware"
	"recruit_student_backend/internal/repository"
)

func main() {
	// 初始化数据库连接
	if err := repository.InitDB(); err != nil {
		log.Fatalf("初始化数据库失败: %v", err)
	}

	// 初始化Gin框架
	r := gin.Default()

	// 配置路由
	students := r.Group("/students")
	{
		students.POST("/", handler.CreateStudent)
		students.GET("/:id", middleware.AuthMiddleware(), handler.GetStudent)
		students.GET("/", middleware.AuthMiddleware(), handler.GetAllStudents)
		students.GET("/page/:page/:pageSize", middleware.AuthMiddleware(), handler.GetStudentsByPage)
		students.PUT("/:id", middleware.AuthMiddleware(), handler.UpdateStudent)
		students.DELETE("/:id", middleware.AuthMiddleware(), handler.DeleteStudent)
	}

	users := r.Group("/users")
	{
		users.POST("/", handler.CreateUser)
		users.GET("/:id", middleware.AuthMiddleware(), handler.GetUser)
		users.GET("/", middleware.AuthMiddleware(), handler.GetAllUsers)
		users.PUT("/:id", middleware.AuthMiddleware(), handler.UpdateUser)
		users.DELETE("/:id", middleware.AuthMiddleware(), handler.DeleteUser)
		users.POST("/login", handler.LoginUser)
	}

	// 启动服务器
	if err := r.Run(":8080"); err != nil {
		log.Fatalf("服务器启动失败: %v", err)
	}
}
